<?php
// 引入头部
require_once 'includes/header.php';

// 获取待处理和制作中的订单
$orders = Database::fetchAll("SELECT o.*, u.name as member_name 
                             FROM orders o 
                             LEFT JOIN users u ON o.user_id = u.id 
                             WHERE o.status IN ('paid', 'cooking') 
                             ORDER BY o.created_at ASC");

// 获取每个订单的详细商品
foreach ($orders as $key => $order) {
    $items = Database::fetchAll("SELECT * FROM order_items WHERE order_id = ?", [$order['id']]);
    $orders[$key]['items'] = $items;
}
?>

<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="d-flex justify-content-between align-items-center">
                <h4>待处理订单</h4>
                <div>
                    <div class="connection-status connecting" id="connection-status">
                        <i class="fas fa-circle"></i> 连接中...
                    </div>
                    <button id="refresh-btn" class="btn btn-info ml-2">
                        <i class="fas fa-sync-alt"></i> 刷新
                    </button>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-12">
            <?php if (empty($orders)): ?>
            <div class="alert alert-info">
                <i class="fas fa-info-circle mr-2"></i> 暂无待处理订单
            </div>
            <?php else: ?>
            <div id="orders-container" class="orders-grid">
                <?php foreach ($orders as $order): ?>
                <div class="order-card" id="order-<?php echo $order['id']; ?>" data-order-id="<?php echo $order['id']; ?>">
                    <div class="order-header">
                        <div class="order-number">订单 #<?php echo $order['queue_number']; ?></div>
                        <div class="order-status status-<?php echo $order['status']; ?>">
                            <?php
                            switch ($order['status']) {
                                case 'paid': echo '待处理'; break;
                                case 'cooking': echo '制作中'; break;
                                default: echo $order['status']; break;
                            }
                            ?>
                        </div>
                    </div>
                    
                    <div class="order-items">
                        <?php foreach ($order['items'] as $item): ?>
                        <div class="order-item" data-item-id="<?php echo $item['id']; ?>">
                            <div class="order-item-name"><?php echo htmlspecialchars($item['product_name']); ?></div>
                            <div class="order-item-quantity">x<?php echo $item['quantity']; ?></div>
                            <div class="order-item-status status-<?php echo $item['status']; ?>">
                                <?php
                                switch ($item['status']) {
                                    case 'pending': echo '待处理'; break;
                                    case 'cooking': echo '制作中'; break;
                                    case 'completed': echo '已完成'; break;
                                    default: echo $item['status']; break;
                                }
                                ?>
                            </div>
                            <div class="order-item-actions">
                                <?php if ($item['status'] !== 'completed'): ?>
                                <button class="btn btn-sm btn-success btn-complete-item" data-order-item-id="<?php echo $item['id']; ?>">
                                    完成
                                </button>
                                <?php else: ?>
                                <span class="text-success"><i class="fas fa-check-circle"></i> 已完成</span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    
                    <div class="order-footer">
                        <div class="order-time">
                            <?php echo date('m-d H:i:s', strtotime($order['created_at'])); ?>
                            <?php if ($order['remark']): ?>
                            <span class="badge badge-warning" title="<?php echo htmlspecialchars($order['remark']); ?>">
                                <i class="fas fa-comment-alt"></i> 备注
                            </span>
                            <?php endif; ?>
                        </div>
                        <div class="order-actions">
                            <?php if ($order['status'] === 'cooking'): ?>
                            <button class="btn btn-success btn-complete-all-items" data-order-id="<?php echo $order['id']; ?>">
                                <i class="fas fa-utensils"></i> 所有菜品完成
                            </button>
                            <button class="btn btn-primary btn-complete-order" data-order-id="<?php echo $order['id']; ?>">
                                完成订单
                            </button>
                            <?php elseif ($order['status'] === 'paid'): ?>
                            <button class="btn btn-primary btn-start-cooking" data-order-id="<?php echo $order['id']; ?>">
                                开始制作
                            </button>
                            <?php else: ?>
                            <button class="btn btn-outline-success" disabled>已完成</button>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<style>
/* 连接状态指示器 */
.connection-status {
    display: inline-block;
    padding: 5px 10px;
    border-radius: 15px;
    font-size: 12px;
    color: white;
    margin-right: 10px;
}

.connection-status.connected {
    background-color: #2ecc71;
}

.connection-status.connecting {
    background-color: #f39c12;
}

.connection-status.disconnected {
    background-color: #e74c3c;
}

/* 加载指示器 */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

.spinner {
    background-color: white;
    padding: 30px;
    border-radius: 10px;
    text-align: center;
}

.spinner p {
    margin-top: 15px;
    font-weight: bold;
}
</style>

<script>
// 刷新按钮点击事件
document.addEventListener('DOMContentLoaded', function() {
    const refreshBtn = document.getElementById('refresh-btn');
    if (refreshBtn) {
        refreshBtn.addEventListener('click', function() {
            this.disabled = true;
            this.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 刷新中...';
            
            // 刷新页面
            window.location.reload();
        });
    }
});
</script>

<?php
// 检查订单项是否都已完成
function allItemsCompleted($items) {
    foreach ($items as $item) {
        if ($item['status'] !== 'completed') {
            return false;
        }
    }
    return true;
}

// 引入页脚
require_once 'includes/footer.php';
?> 